package com.wosiliujing.learning.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wosiliujing.learning.admin.dto.UserDto;
import com.wosiliujing.learning.admin.dto.UserInfo;
import com.wosiliujing.learning.admin.dto.UserRestPasswordDto;
import com.wosiliujing.learning.admin.po.SysUser;
import com.wosiliujing.learning.annotation.Inner;
import com.wosiliujing.learning.annotation.SysLog;
import com.wosiliujing.learning.constant.CommonConstants;
import com.wosiliujing.learning.service.SysUserService;
import com.wosiliujing.learning.util.R;
import com.wosiliujing.learning.util.SecurityUtil;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author liujing
 */
@RequestMapping("/user")
@RestController
@AllArgsConstructor
public class SysUserController {

    private final SysUserService sysUserService;

    /**
     * 获取当前用户
     * @return 当前用户信息
     */
    @GetMapping("/info")
    public R info(){
        String userName = SecurityUtil.getUser();
        SysUser sysUser = sysUserService.getOne(
                Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUserName,userName));
        if(ObjectUtil.isEmpty(sysUser)){
            return new R<>();
        }
        UserInfo userInfo = sysUserService.getUserInfo(sysUser);
        return R.data(userInfo);
    }


    /**
     * 通过用户名获取用户
     * @param userName 用户名
     * @return 用户信息
     */
    @Inner
    @RequestMapping("/info/{userName}")
    public R info(@PathVariable String userName){
        SysUser sysUser = sysUserService.getOne(
                Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUserName,userName));
        if(ObjectUtil.isEmpty(sysUser)){
            return new R<>();
        }
        UserInfo userInfo = sysUserService.getUserInfo(sysUser);
        return R.data(userInfo);
    }

    /**
     * 通过id获取用户展示信息
     * @param id 用户id
     * @return 用户信息
     */
    @GetMapping("/vo/{id}")
    public R getUserVoById(@PathVariable String id){
     return R.data(sysUserService.getUserVoById(id));
    }



    /**
     * 通过id获取用户详情
     * @param id 用户id
     * @return 用户详情
     */
    @GetMapping("/detail/{id}")
    public R getUserDetailById(@PathVariable String id){
        return R.data(sysUserService.getById(id));
    }

    /**
     * 新增用户
     * @param userDto 用户新增信息
     * @return 保存结果
     */
    @SysLog("新增用户")
    @PreAuthorize("@pms.hasPermisssion('sys_user_add')")
    @PostMapping("add")
    public R addUser(@RequestBody UserDto userDto){
        return  sysUserService.saveUser(userDto)?R.ok():R.error(CommonConstants.SAVE_FAIL);
    }

    /**
     * 删除用户
     * @param id 用户id
     * @return 删除结果
     */
    @SysLog("删除用户")
    @PreAuthorize("@pms.hasPermisssion('sys_user_del')")
    @DeleteMapping("delete/{id}")
    public R deleteUser(@PathVariable String id){
        return sysUserService.deleteUser(id)?R.ok():R.error(CommonConstants.DELETE_FAIL);
    }


    /**
     * 分页查询用户展示信息
     * @param page 分页参数
     * @param userDto 查询条件
     * @return 分页数据
     */
    @GetMapping("/page")
    public R getUserVoPage(Page page, UserDto userDto){
        return R.data(sysUserService.getUserVoPage(page, userDto));
    }

    /**
     * 更新用户信息
     * @param userDto 更新参数
     * @return 更新结果
     */
    @SysLog("更新用户")
    @PreAuthorize("@pms.hasPermisssion('sys_user_edit')")
    @PutMapping("/update")
    public R updateUser(@RequestBody UserDto userDto){
        return sysUserService.updateUser(userDto)?R.ok():R.error(CommonConstants.UPDATE_FAIL);
    }

    /**
     * 重置用户密码
     * @param userRestPasswordDto 新旧密码
     * @return 重置结果
     */
    @SysLog("重置用户密码")
    @PreAuthorize("@pms.hasPermisssion('sys_user_rest_password')")
    @PutMapping("/restPassword")
    public R restPassword(@RequestBody UserRestPasswordDto userRestPasswordDto){
        return sysUserService.restPassword(userRestPasswordDto);
    }

    /**
     * 重置当前用户密码
     * @param userRestPasswordDto 新旧密码
     * @return 重置结果
     */
    @SysLog("重置当前用户密码")
    @PutMapping("/restOwnPassword")
    public R restOwnPassword(@RequestBody UserRestPasswordDto userRestPasswordDto){
        return sysUserService.restOwnPassword(userRestPasswordDto);
    }

    /**
     * 更新本人用户信息
     * @param userDto 用户信息
     * @return 更新结果
     */
    @SysLog("更新本人用户信息")
    @PutMapping("/updateOwnInfo")
    public R updateOwnInfo(@RequestBody UserDto userDto){
        return sysUserService.updateOwnInfo(userDto)?R.ok():R.error(CommonConstants.UPDATE_FAIL);
    }

}
